/**
 * Created by xxx on 2017/7/12.
 * 新建相册
 */
'use strict';
import React, {
    Component
} from 'react'
import {
    Link
} from 'react-router'
import {
    hashHistory
} from 'react-router'

import Header from './Common/Header'
import {
    HttpService,
    getLocalTime,
    Tips,
    specialStr
} from '../Tools'
/*
 *班级相册-教师、家长
 */
export default class ScoreDetail extends Component {
    componentDidMount() {
        if (this.props.params.edit == 'true') {
            this.refs.bookname.value = decodeURIComponent(this.props.params.name)
            this.refs.bookremark.value = decodeURIComponent(this.props.params.content)
        }
    }

    //保存相册
    addAlbum() {
        const bookremark = this.refs.bookremark.value,
            bookname = this.refs.bookname.value;
        if (bookremark == '' || bookname == '') {
            Tips('请填写相册名称及描述 ')
            return;
        }
        if (specialStr(bookremark) || specialStr(bookname)) {
            Tips('名称及描述不能有特殊字符')
            return;
        }
        if (bookname.length > 12) {
            Tips('相册名称已超过12字')
            return;
        }
        if (bookremark.length > 50) {
            Tips('相册描述已超过50字')
            return;
        }
        let data = {
            jybTicket: sessionStorage.ticket,
            method: 'ajaxJybJSON_101AddClassPhotoBook',
            classid: this.props.params.id,
            bookremark,
            bookname
        }
        if (this.props.params.edit == 'true') { //编辑
            data.method = 'ajaxJybJSON_101AmendClassPhotoBookName'
            data.bookid = this.props.params.bookid
        }
        HttpService.query({
            url: '/jxt-nyxt/ajaxClassPhotoBookDealer.do',
            data,
            async: false,
            success: (res => {
                if (res.code == 0) {
                    if (this.props.params.edit == 'true') {
                        hashHistory.push('/AlbumDetail/' + this.props.params.bookid + '/' + this.props.params.id + '/' + encodeURIComponent(bookname) + '/' + encodeURIComponent(bookremark) + '/' + true);
                    } else {
                        hashHistory.goBack(-1);
                    }
                } else {
                    Tips(res.msg || '操作失败');
                }
            })
        })
    }

    render() {
        const publishLink = <a className='h_link color_blue' onClick={this.addAlbum.bind(this)}>保存</a>;
        return (
            <div className='add_album'>
                <Header title={this.props.params.edit == 'true'
                    ? '编辑相册'
                    : '新建相册'} link={publishLink}/>
                <div className="centent">
                    <div className="input_value">
                        <div className="title">相册名称</div>
                        <input type="text" ref='bookname' placeholder="请输入相册名称（12字以内）"/>
                    </div>
                    <div className="input_value">
                        <div className="title">相册描述</div><textarea rows='3' ref='bookremark' placeholder="请输入照片描述（50字以内）"/>
                    </div>
                </div>
            </div>
        )
    }
}
